package com.mtnz.njk.Util;

import cn.hutool.core.util.IdUtil;
import com.mtnz.njk.Mapper.AskMapper;
import com.mtnz.njk.Mapper.CommunityUserMapper;
import com.mtnz.njk.Mapper.NewsMapper;
import com.mtnz.njk.entity.AskEntity;
import com.mtnz.njk.entity.NewsEntity;
import com.mtnz.njk.entity.NjkUserEntity;
import com.mtnz.njk.model.UserAskModel;

import javax.annotation.Resource;

public class CommentAsk implements Runnable {

    public String id;
    public String userId;
    public String massage;
    public String url;
    @Resource(name = "askMapper")
    public AskMapper askMapper;
    @Resource(name = "userMapper")
    public CommunityUserMapper userMapper;
    @Resource(name = "newsMapper")
    public NewsMapper newsMapper;

    public CommentAsk(String askId, String massage, String userId, AskMapper askMapper, CommunityUserMapper userMapper, NewsMapper newsMapper, String url) {
        this.id = askId;
        this.userId = userId;
        this.massage = massage;
        this.askMapper = askMapper;
        this.userMapper = userMapper;
        this.newsMapper = newsMapper;
        this.url = url;
    }

    @Override
    public synchronized void run() {
        // TODO Auto-generated method stub
        try {
            AskEntity askEntity = new AskEntity();
            askEntity.setAskId(id);
            askEntity.setMessage(massage);
            askEntity.setUserId(userId);
            UserAskModel byId = askMapper.findById(id);
            NjkUserEntity byId1 = userMapper.findById(userId);
            //保存消息
            NewsEntity newsEntity = new NewsEntity();
            newsEntity.setNewsId(IdUtil.simpleUUID());
            newsEntity.setId(askEntity.getAskId());
            newsEntity.setMessage(massage);
            newsEntity.setName(byId1.getNickName());
            newsEntity.setImg(byId1.getImg());
            newsEntity.setCreateDate(cn.hutool.core.date.DateUtil.now());
            newsEntity.setUserId(byId.getUserId());
            newsEntity.setSubject(byId.getMessage());
            newsEntity.setStatus("1");
            newsEntity.setUserId1(askEntity.getUserId());
            newsEntity.setUrl(url);
            newsMapper.insert(newsEntity);
            NjkUserEntity njkUserEntity = userMapper.selectByPrimaryKey(userId);
            njkUserEntity.setStatus("1");
            userMapper.updateByPrimaryKeySelective(njkUserEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
